-------------------------------
--     Title Card HUD v2     --
--     by EeveeEuphoria      --
-------------------------------

-- NOTICE! This is primarily for internal use in the Castle Insania episode! There will be no documentation for this. --


local textplus = require("textplus")

local titlecardHUD = {}

local TICRATE = 61 -- yes i am declaring a ticrate variable, shush
local waittimer = 0
local TCtimer = 0
local maxtime = 2.75 * TICRATE -- time to display the title card

local Header = Graphics.loadImage(Misc.resolveFile("GFX/header.png"))
local HardModeGFX = Graphics.loadImage(Misc.resolveFile("GFX/hard mode incomplete.png"))
local HardModeGFX2 = Graphics.loadImage(Misc.resolveFile("GFX/hard mode.png"))
local HeaderName = "regular"

local yoffset = -Header.height
local xoffset = 0

local DoNotRun = false

local GoodFont = textplus.loadFont("Fonts/Enjl/6.ini")

local DefaultHeader = "GFX/header.png"

function titlecardHUD.onInitAPI()
    registerEvent(titlecardHUD, "onStart")
	registerEvent(titlecardHUD, "onDraw")
	registerEvent(titlecardHUD, "onTick")
end

local TotalCollectedStars = 0

function titlecardHUD.onStart() -- get current starcoin count, and difference count
	if not LevelsTable then
		error("LevelsTable variable can't be found!")
	end
	if titlecardHUD.exceptions[Level.filename()] then
		DoNotRun = true
	end

	TotalCollectedStars = getCollectedStarCount(Level.filename())

	if SearchLevelName(Level.filename()).world == 1 then
		DefaultHeader = "GFX/header 1.png"
	elseif SearchLevelName(Level.filename()).world == 2 then
		DefaultHeader = "GFX/header 2.png"
	end
	Header = Graphics.loadImage(Misc.resolveFile(DefaultHeader))
end

function titlecardHUD.onTick()
	if DoNotRun then
		return
	end

	if GameData.TransFlag and HeaderName == "regular" then
		Header = Graphics.loadImage(Misc.resolveFile("GFX/trans header.png"))
		HeaderName = "trans"
	elseif not GameData.TransFlag and HeaderName == "trans" then
		Header = Graphics.loadImage(Misc.resolveFile(DefaultHeader))
		HeaderName = "regular"
	end

	if waittimer < TICRATE/2 then
		waittimer = waittimer + 1
	else
		TCtimer = TCtimer + 1
		if TCtimer < maxtime then
			if yoffset < -2 then
				yoffset = yoffset + (math.sin(((TCtimer)/20)) * 5)
			else
				yoffset = 0
			end
		else
			if yoffset > -Header.height then
				yoffset = yoffset - (math.sin(((TCtimer-maxtime)/20)) * 5)
			else
				yoffset = -Header.height
				DoNotRun = true
			end
		end
		xoffset = xoffset - 2
		--Text.print(math.floor(yoffset), 20, 220)
	end
end

function titlecardHUD.onDraw()
	if DoNotRun then
		return
	end
	Graphics.drawImageWP(Header, xoffset+(Header.width*0), yoffset, 0.8, 15)
	Graphics.drawImageWP(Header, xoffset+(Header.width*1), yoffset, 0.8, 15)
	Graphics.drawImageWP(Header, xoffset+(Header.width*2), yoffset, 0.8, 15)
	Graphics.drawImageWP(Header, xoffset+(Header.width*3), yoffset, 0.8, 15)
	Graphics.drawImageWP(Header, xoffset+(Header.width*4), yoffset, 0.8, 15)
	if globalsave.HardModePurchase then
		local Gamer = HardModeGFX
		if globalsave.HMLevelsBeat[Level.filename()] then
			Gamer = HardModeGFX2
		end
		Graphics.drawImageWP(Gamer, 750, yoffset + ((Header.height-8)/2) - (Gamer.height/2), 0.8, 15)
	end
	local xoffsetTitle = 0
	local EmblemGFX
	if not EmblemGFX and SearchLevelName(Level.filename()).emblem ~= nil then
		EmblemGFX = Graphics.loadImage(Misc.resolveFile(tostring(SearchLevelName(Level.filename()).emblem)))
	end
	if EmblemGFX then
		local yPos = ((Header.height-8)/2) - (EmblemGFX.height/2)
		xoffsetTitle = EmblemGFX.width
		Graphics.drawImageWP(EmblemGFX, 24, yoffset + yPos, 0.8, 15)
		--Text.print(yoffset+yPos, 20, 400)
	end
	local PreText = "<size 4><wave 3>"
	local PostText = "</wave></size>"
	--[[if globalsave.LevelsBeat[Level.filename()] then
		PreText = "<size 4>"
		PostText = "</size>"
	end]]
	textplus.print{ -- level name
		x = 48 + xoffsetTitle,
		y = yoffset + ((Header.height-8)/4),
		text = PreText .. tostring(SearchLevelName(Level.filename()).name) .. PostText,
		font = GoodFont,
		priority = 16
	}
	if mem(0x00B2C8A8,FIELD_WORD) > 0 then -- star count
		textplus.print{
			x = 48 + xoffsetTitle,
			y = yoffset + ((Header.height+8)/2),
			text = "<size 2>🌟 ✕ " .. TotalCollectedStars .. "/ " .. mem(0x00B2C8A8,FIELD_WORD) .. "</size>",
			font = GoodFont,
			priority = 16
		}
	end
end

return titlecardHUD